package com.czy.owner.ui.store;


import android.content.Intent;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.czy.owner.R;
import com.czy.owner.adapter.StoreAdapter;
import com.czy.owner.api.StoreActivityApi;
import com.czy.owner.callback.RecycleViewDivider;
import com.czy.owner.db.UserHelper;
import com.czy.owner.entity.CaseShowModel;
import com.czy.owner.entity.StoreActivityModel;
import com.czy.owner.global.Constants;
import com.czy.owner.net.api.BaseResultEntity;
import com.czy.owner.net.http.HttpManager;
import com.czy.owner.net.listener.HttpOnNextListener;
import com.czy.owner.ui.BaseActivity;
import com.czy.owner.ui.information.InformationDetailActivity;
import com.czy.owner.utils.PhoneUtils;
import com.czy.owner.utils.SharedPreferencesUtils;
import com.easyrecycleview.EasyRecyclerView;
import com.easyrecycleview.adapter.RecyclerArrayAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;

import butterknife.BindView;


/**
 * Created by wangyang on 2016/6/3.
 * 门店活动
 */
public class StoreActivity extends BaseActivity implements RecyclerArrayAdapter.OnLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.easy_recycleview)
    EasyRecyclerView recyclerView;
    private StoreAdapter adapter;
    private int page = 1;
    private Handler handler = new Handler();

    @Override
    protected int attachLayoutRes() {
        return R.layout.common_activity_easyrecycleview;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        getToolbarTitle().setText(R.string.store_activity);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapterWithProgress(adapter = new StoreAdapter(this));
        recyclerView.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.VERTICAL, R.drawable.shape_shopping_strategy_divider_decoration));
        recyclerView.setErrorView(R.layout.view_error_net);
        adapter.setMore(R.layout.view_more, this);
        adapter.setNoMore(R.layout.view_nomore, new RecyclerArrayAdapter.OnNoMoreListener() {
            @Override
            public void onNoMoreShow() {
                // adapter.resumeMore();
            }

            @Override
            public void onNoMoreClick() {
                adapter.resumeMore();
            }
        });


        adapter.setError(R.layout.view_error_net, new RecyclerArrayAdapter.OnErrorListener() {
            @Override
            public void onErrorShow() {
                adapter.resumeMore();

            }

            @Override
            public void onErrorClick() {
                adapter.resumeMore();

            }
        });

        recyclerView.setRefreshListener(this);

        onRefresh();
        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener<StoreActivityModel.StoreList>() {
            @Override
            public void onItemClick(View view, int position, StoreActivityModel.StoreList model) {

                Intent ti = new Intent(StoreActivity.this, InformationDetailActivity.class);
                ti.putExtra("title", model.getTitle());
                ti.putExtra("content", model.getContent());
                ti.putExtra("infoId", model.getActivityId());
                ti.putExtra("collectType", "storeActivity");
                try {
                    if (model.getResList().size() > 0) {
                        ti.putExtra("imgUrl", model.getResList().get(0).getThumbnailUrl());
                    }
                } catch (Exception e) {

                }
                startActivity(ti);


            }
        });
    }

    @Override
    protected void updateViews() {

    }


    @Override
    public void onLoadMore() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                //刷新
                if (!PhoneUtils.isNetworkConnected(StoreActivity.this)) {
                    adapter.pauseMore();
                    recyclerView.showError();
                    return;
                }
                page++;
                getDataList();

            }
        }, 1000);
    }

    @Override
    public void onRefresh() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                //刷新
                adapter.clear();
                page = 1;
                getDataList();
            }
        }, 1000);
    }


    private void getDataList() {
        StoreActivityApi storeActivityApi = new StoreActivityApi(new HttpOnNextListener<StoreActivityModel>() {
            @Override
            public void onNext(StoreActivityModel model) {
                setDataList(model);
            }

            @Override
            public void onCacheNext(String cache) {
                super.onCacheNext(cache);
                Gson gson = new Gson();
                Type type = new TypeToken<BaseResultEntity<CaseShowModel>>() {
                }.getType();
                BaseResultEntity<StoreActivityModel> cacheList = gson.fromJson(cache, type);
                setDataList(cacheList.getData());
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                PhoneUtils.ShowToastMessage(StoreActivity.this, e.getMessage());
            }
        }, this, page + "");
        storeActivityApi.setSession(UserHelper.getInstance().getSessionInfoModel(StoreActivity.this).getSession());
        storeActivityApi.setStoreId(SharedPreferencesUtils.getParam(StoreActivity.this, UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_BINDSTOREID, 0) + "");
        storeActivityApi.setSize("10");
        storeActivityApi.setPage(page + "");
        HttpManager httpManager = HttpManager.getInstance();
        httpManager.doHttpDeal(storeActivityApi);
    }


    private void setDataList(StoreActivityModel model) {
        if (model.getList().size() < 9) {
            adapter.stopMore();
        }
        adapter.addAll(model.getList());
        if (adapter.getAllData().size() == 0) {
            recyclerView.setEmptyView(R.layout.view_empty_common);
            recyclerView.showEmpty();
        }
    }


}
